import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:rabbit_mobile/view/Login/index.dart';
import 'package:rabbit_mobile/view/Oder_payment/index.dart';
import 'package:rabbit_mobile/view/SMS_verification_code/index.dart';
import 'package:rabbit_mobile/view/goods_details/index.dart';
import 'package:rabbit_mobile/view/register/index.dart';
import 'package:rabbit_mobile/view/register_in/index.dart';
import 'package:rabbit_mobile/widget/child_home/index.dart';
import '../view/UserProfilePage/index.dart';
import '../view/webview_flutter/index.dart';
import './index.dart';

class PathLocation extends BeamLocation<BeamState> {
  @override
  List<Pattern> get pathPatterns => [
        '/',
        '/category',
        '/shopping_cart',
        '/mine',
        '/child_home/:id',
        '/login',
        '/register',
        '/SMSVerificationCode/:id',
        '/registerin/:mobile/:code',
        '/goodsDetails/:id',
        'webviewWidget',
        '/oder_payment',
      ];

  @override
  List<BeamPage> buildPages(BuildContext context, BeamState state) {
    final List<BeamPage> pages = [];
    final String? id = state.pathParameters['id'];

    if (state.uri.pathSegments.isEmpty || state.uri.pathSegments[0] == '/') {
      // 如果是根路径 '/'
      pages.add(
        const BeamPage(
            key: ValueKey('home'),
            child: Routers(
              initialIndex: 0,
            ),
            type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('category')) {
      // 如果路径是 '/category'
      pages.add(
        const BeamPage(
            key: ValueKey('category'),
            child: Routers(
              initialIndex: 1,
            ),
            type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('shopping_cart')) {
      // 如果路径是 '/shopping_cart'
      pages.add(
        const BeamPage(
            key: ValueKey('shopping_cart'),
            child: Routers(
              initialIndex: 2,
            ),
            type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('mine')) {
      // 如果路径是 '/mine'
      pages.add(
        const BeamPage(
            key: ValueKey('mine'),
            child: Routers(
              initialIndex: 3,
            ),
            type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('child_home') && id != null) {
      // 如果路径是 '/child_home'
      pages.add(
        BeamPage(
            key: const ValueKey('child_home'),
            child: ChildHome(id: id), // 这里是你定义的页面
            type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('login')) {
      pages.add(
        const BeamPage(key: ValueKey('login'), child: Login(), type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('register')) {
      pages.add(
        const BeamPage(key: ValueKey('register'), child: Register(), type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('SMSVerificationCode') && id != null) {
      pages.add(
        BeamPage(
            key: const ValueKey('/SMSVerificationCode'),
            child: SMSVerificationCode(
              phoneNumber: id,
            ),
            type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('registerin') &&
        state.pathParameters.containsKey('mobile') &&
        state.pathParameters.containsKey('code')) {
      final mobile = state.pathParameters['mobile'];
      final code = state.pathParameters['code'];

      pages.add(
        BeamPage(
            key: const ValueKey('registerin'),
            child: RegisterIn(phoneNumber: mobile!, code: code!),
            type: BeamPageType.cupertino),
      );
    }
    if (state.uri.pathSegments.contains('goodsDetails') && id != null) {
      pages.add(
        BeamPage(
          key: const ValueKey('goodsDetails'),
          child: GoodsDetails(id: id),
          type: BeamPageType.cupertino,
        ),
      );
    }
    if (state.uri.path.contains('oder_payment')) {
      pages.add(
        const BeamPage(
          key: ValueKey('oder_payment'),
          child: OrderPayment(),
          type: BeamPageType.cupertino,
        ),
      );
    }
    // if (state.uri.pathSegments.contains('webviewWidget')) {
    //   pages.add(
    //     BeamPage(
    //       key: const ValueKey('goodsDetails'),
    //       child: MyWebViewPage(),
    //       type: BeamPageType.cupertino,
    //     ),
    //   );
    // }
    // 如果没有匹配到任何页面，展示 404 错误页面
    if (pages.isEmpty) {
      pages.add(
        const BeamPage(
          key: ValueKey('error'),
          child: Scaffold(body: Center(child: Text('Error: Unknown Page'))),
        ),
      );
    }
    return pages;
  }
}
